<template>
  <div>
    <el-form :inline="true" :model="cateGoryId" class="demo-form-inline">
      <!-- 一级分类 -->
      <el-form-item label="一级分类" style="margin-left: 10px">
        <el-select
          placeholder="请选择"
          v-model="cateGoryId.cateGory1Id"
          @change="getCategory2List"
        >
          <el-option
            :label="c1.name"
            :value="c1.id"
            v-for="c1 in cateGory.category1List"
            :key="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- 二级分类 -->
      <el-form-item label="二级分类" style="margin-left: 15px">
        <el-select
          placeholder="请选择"
          v-model="cateGoryId.cateGory2Id"
          @change="getCategory3List"
        >
          <el-option
            :label="c2.name"
            :value="c2.id"
            v-for="c2 in cateGory.category2List"
            :key="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- 三级分类 -->
      <el-form-item label="三级分类" style="margin-left: 15px">
        <el-select
          placeholder="请选择"
          v-model="cateGoryId.cateGory3Id"
          @change="$emit('getCategoryId', cateGoryId)"
        >
          <el-option
            :label="c3.name"
            :value="c3.id"
            v-for="c3 in cateGory.category3List"
            :key="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
export default {
  name: "CategorySelect",
  components: {},
  data() {
    return {
      shanghai: "",
      cateGory: {
        category1List: [],
        category2List: [],
        category3List: [],
      },
      cateGoryId: {
        cateGory1Id: "",
        cateGory2Id: "",
        cateGory3Id: "",
      },
    };
  },
  methods: {
    //获取一级列表数据
    async getCategory1List() {
      let result = await this.$API.attr.reqCaregroy1List();
      if (result.code === 200) {
        this.cateGory.category1List = result.data;
      }
    },
    //获取二级分类列表
    async getCategory2List() {
      this.cateGory.category2List = [];
      this.cateGory.category3List = [];
      this.cateGoryId.cateGory2Id = "";
      this.cateGoryId.cateGory3Id = "";
      let result = await this.$API.attr.reqCaregroy2List(
        this.cateGoryId.cateGory1Id
      );
      if (result.code === 200) {
        this.cateGory.category2List = result.data;
      }
    },
    //获取三级分类列表
    async getCategory3List() {
      this.cateGory.category3List = [];
      this.cateGoryId.cateGory3Id = "";
      let result = await this.$API.attr.reqCaregroy3List(
        this.cateGoryId.cateGory2Id
      );
      if (result.code === 200) {
        this.cateGory.category3List = result.data;
      }
    },
  },
  mounted() {
    this.getCategory1List();
  },
};
</script>


<style scoped>
</style>